package com.java.service;

import com.java.dao.EnterGoodsMapper;
import com.java.dao.Maker1Mapper;
import com.java.dao.Maker2Mapper;
import com.java.pojo.Maker1;
import com.java.pojo.Maker2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Maker2ServiceImpl  implements Maker2Service{


    @Autowired
    private Maker2Mapper maker2Mapper;

    @Autowired
    private Maker1Mapper maker1Mapper;
    @Autowired
    private EnterGoodsMapper enterGoodsMapper;


    @Override
    public List<Maker2> getMaker2ListByM1id(Integer mid) {
        return maker2Mapper.getMaker2ListByM1id(mid);
    }

    @Override
    public int ADDMaker2(Maker2 maker2) {
        return maker2Mapper.ADDMaker2(maker2);
    }

    //删除
    @Override
    public int deleteMaker2(Integer m1Id) {
        //获取maker2
        Maker2 maker2 = maker2Mapper.getMaker2ByM2Id(m1Id);
        //获取外键
        Integer m1id = maker2.getM1id();
        //根据外键查看maker1
        Maker1 maker1 = maker1Mapper.getMaker1ByMId(m1id);
        System.out.println("maker1:"+maker1+",maker2:"+maker2);



        //获取maker1和maker2的数据
        //maker1
        Integer num = maker1.getNum();
        Double money = maker1.getMoney();
        String goodname = maker1.getGoodname();
        //maker2
        Integer num1 = maker2.getNum();
        Double discountmoney = maker2.getDiscountmoney();
        String goods2 = maker2.getGoods2();


        //创建一个maker1
        Maker1 maker11=new Maker1();
        //创建三个变量
        Integer nwNum=0;
        String nwGoodName="";
        Double nwMoney=0.0;

        nwNum=num-num1;
        nwMoney=money-discountmoney;
        nwGoodName=goodname.replace(goods2,"");
        System.out.println("nwGoodName"+nwGoodName);

        //赋值
        maker11.setNum(nwNum);
        maker11.setGoodname(nwGoodName);
        maker11.setMoney(nwMoney);
        maker11.setMid(m1id);

        //修改删除操作
        int i = maker1Mapper.updMaker1(maker11);
        int id = maker2Mapper.delMaker2ByM2Id(m1Id);
        int i1 = enterGoodsMapper.updOknandNo3_2(goods2);

        return i+id;
    }

    //修改
    @Override
    public int updMaker2(Integer m2id,Integer num, Double money, String disCount) {
        //根据主键查询maker2
        Maker2 maker2 = maker2Mapper.getMaker2(m2id);
        //根据外键查询
        Maker1 maker1 = maker1Mapper.getMaker1ByMId(maker2.getM1id());


        //计算  折扣后的单价 折扣后的总价格  没有打折的总价格
        Double dis=0.0;
        dis=Double.parseDouble(disCount)/10;//折扣  0.几
        Double disMoney=dis*money;  //折扣后的单价
        double disCount2 = disMoney * num;//折扣后总价
        Double count=num*money;//没有折扣的总价格
        //创建一个maker2,修改maker2
        Maker2 maker21=new Maker2();
        maker21.setM2id(m2id);
        maker21.setNum(num);
        maker21.setDiscount(Integer.parseInt(disCount)); maker21.setOnemoney(money);
        maker21.setNowmoney(disMoney); maker21.setDiscountmoney(disCount2);
        maker21.setCountmoney(count);

        //创建maker1,修改maker1用
        Maker1 maker11=new Maker1();
        Integer num1=maker1.getNum()-maker2.getNum()+num;
        Double money1=maker1.getMoney()-maker2.getDiscountmoney()+disCount2;
        maker11.setNum(num1);
        maker11.setMoney(money1);
        maker11.setMid(maker2.getM1id());


        //操作,修改maker2和maker1
        int i1 = maker1Mapper.updMaker1(maker11);
        int i = maker2Mapper.updMaker2(maker21);


        return i1+i;
    }

    //根据主键查询
    @Override
    public Maker2 getMak2ByM2id(Integer id) {
        return maker2Mapper.getMaker2(id);
    }

    @Override
    public List<Maker2> getMaker2List(Integer m2id) {
        return maker2Mapper.getMaker2List(m2id);
    }

}
